<?php

/**
 * 
 * @since 15.04.12
 * @author Arsen Abdusalamov <duburlan@gmail.com>
 */  

class ContentUrlRule extends CBaseUrlRule {
	
	public $connectionID = 'db';
	
	/**
	 * 
	 * @since 15.04.12 20:16
	 * @author Arsen Abdusalamov <duburlan@gmail.com>
	 * 
	 * @param type $manager
	 * @param string $route
	 * @param array $params
	 * @param type $ampersand 
	 * @return string|bool 
	 */
	public function createUrl( $manager, $route, $params, $ampersand ) {
		$result = false;
		if ( $route == 'content/view' && isset( $params['id'] ) && $content = Content::model()->findByPk( $params['id'] ) ) {
			$result = $content->slug ?: false;
		}
		
		return $result;  // не применяем данное правило
	}
	/**
	 * 
	 * @since 15.04.12 20:17
	 * @author Arsen Abdusalamov <duburlan@gmail.com>
	 *
	 * @param type $manager
	 * @param type $request
	 * @param type $pathInfo
	 * @param type $rawPathInfo
	 * @return string|bool 
	 */
	public function parseUrl( $manager, $request, $pathInfo, $rawPathInfo ) {
		$result = false;	
		if ( $content = Content::model()->findByAttributes( array( 'slug' => $pathInfo ) ) ) {
			$result = '/content/view/id/' . $content->getPrimaryKey();
        }
		return $result;
	}

}
